package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LogoutServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        req.getSession().invalidate();

        String url = req.getContextPath() + "/";
        Cookie c = new Cookie("account", "");
        c.setPath(url);
        c.setMaxAge(0);
        resp.addCookie(c);
        c = new Cookie("password","");
        c.setPath(url);
        c.setMaxAge(0);
        resp.addCookie(c);

        req.getSession().setAttribute("userName", "登录");
        req.getSession().setAttribute("userJudge", "注册");
        resp.sendRedirect("index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
